<template>
  <div class="wchangeOd">
    <div class="mask"></div>
    <div class="changeod">
      <div>
        <p>改单(仓库盘点查询)</p>
        <i class="el-icon-close" @click="closewod"></i>
      </div>
      <div class="c-content">
        <div>
          <el-button @click="query" type="primary" icon="el-icon-search"
            >查询</el-button
          >
          <el-button @click="odChange" icon="el-icon-edit-outline"
            >修改</el-button
          >
        </div>
        <div>
          <p>盘点日期</p>
          <!-- <el-input v-model="checkDate" @blur="leave"></el-input> -->
          <el-date-picker
            class="build-inp"
            v-model="checkDate"
            value-format="yyyy-MM-dd"
            @change="leave"
            type="date"
          >
          </el-date-picker>
          <p>单据号</p>
          <el-input v-model="receiptsNumber" @input="leave"></el-input>
          <p>仓库编号</p>
          <!-- <el-input v-model="warehouseNo" @blur="leave"></el-input> -->
          <el-select v-model="warehouseNo" @change="leave">
            <el-option
              v-for="item in warehouseNoList"
              :key="item.value"
              :label="item.value"
              :value="item.value"
            >
              <span style="float:left">{{ item.value }}</span>
              <span style="float:right">{{ item.label }}</span>
            </el-option>
          </el-select>
          <p>盘点数量</p>
          <el-input
            v-model="stockingNumber"
            @blur="leave1"
            :disabled="Dis"
          ></el-input>
        </div>
        <div>
          <p>款号</p>
          <el-input v-model="kindsNumber"></el-input>
          <p>售价</p>
          <el-input disabled v-model="sellingPrice"></el-input>
          <p>盘点单价</p>
          <el-input disabled v-model="onePrice"></el-input>
          <p>盘点序号</p>
          <el-input disabled v-model="sequence"></el-input>
        </div>
        <div>
          <p>仓库名称</p>
          <el-input disabled v-model="warehouseName"></el-input>
        </div>

        <el-table
          :data="tabList"
          highlight-current-row
          height="160px"
          @row-click="rowClick"
          :header-cell-style="{
            background: '#f4f4f4',
            padding: 0,
            height: '40px'
          }"
          :cell-style="{ padding: 0, height: '36px' }"
        >
          <el-table-column
            prop="checkTime"
            label="盘点日期"
            :show-overflow-tooltip="true"
          >
            <!-- <template slot-scope="scope"> -->
            <div slot="content">123</div>
            <!-- </template> -->
          </el-table-column>
          <el-table-column prop="receiptsNumber" label="单据号">
          </el-table-column>
          <el-table-column prop="warehouseNo" label="仓库编号" title="123">
          </el-table-column>
          <el-table-column prop="stockingNumber" label="盘点数量">
          </el-table-column>
        </el-table>

        <div>
          <el-button @click="closewod">取消</el-button>
          <el-button type="primary" @click="Save">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getWarehouse } from '../../../../network/warehouse'
export default {
  props: {
    showData: {
      type: Object,
      default: {}
    }
  },
  created() {
    console.log(this.showData)
    // 获取仓库信息
    getWarehouse().then(res => {
      console.log(res.data)
      let list = res.data.data
      list.sort((a, b) => {
        return a.no.localeCompare(b.no)
      })
      list.forEach(e => {
        this.warehouseNoList.push({ value: e.no, label: e.name })
        // if (e.isMainWarehouse) {
        //     this.warehouseNo = e.no
        //     this.warehouseName = e.name
        // }
      })
    })
  },
  data() {
    return {
      Dis: false,
      saveList: [],
      tabList: [],
      checkDate: '',
      warehouseNoList: [],
      receiptsNumber: '',
      warehouseNo: '',
      totalNumber: '',
      kindsNumber: '',
      sellingPrice: '',
      onePrice: '',
      sequence: '',
      stockingNumber: '',
      warehouseName: '',
      cc: '',
      num: ''
    }
  },
  methods: {
    //按货号查询
    query() {
      if (this.kindsNumber == '') {
        this.$message({
          type: 'warning',
          message: '请输入货号查询'
        })
      }
      for (let t of this.tabList) {
        if (this.kindsNumber == t.kindsNumber) {
          this.tabList = [t]
        }
      }
    },
    closewod() {
      this.$emit('closewod')
    },
    rowClick(e) {
      console.log(e)
      // localStorage.setItem('pdChangeList',JSON.stringify(this.tabList))
      if (e.dataSource == 0) {
        this.Dis = true
      } else if (e.dataSource == 1) {
        this.Dis = false
      }
      this.stockingNumber = e.stockingNumber
      this.kindsNumber = e.kindsNumber
      this.sellingPrice = e.sellingPrice
      this.onePrice = e.onePrice
      this.receiptsNumber = e.receiptsNumber
      this.checkDate = e.checkTime.slice(0, 10)
    },
    leave() {
      // for(let w of this.saveList){
      //   if(this.kindsNumber != undefined){
      //       w.warehouseNo = this.warehouseNo
      //       w.receiptsNumber = this.receiptsNumber
      //       w.checkTime = this.checkDate
      //     for(let r of this.warehouseNoList){
      //         if(this.warehouseNo == r.value){
      //             this.warehouseName = r.label
      //         }
      //     }
      //   }
      // }

      for (let q of this.tabList) {
        if (this.kindsNumber != undefined) {
          q.warehouseNo = this.warehouseNo
          q.receiptsNumber = this.receiptsNumber
          q.checkTime = this.checkDate
          for (let s of this.warehouseNoList) {
            if (this.warehouseNo == s.value) {
              this.warehouseName = s.label
            }
          }
        }
      }
    },
    leave1() {
      //盘点数量

      // this.num = JSON.parse(localStorage.getItem('pdChangeList'))  //查 库存是否足够扣库存  暂不用
      // // console.log(this.num)
      // if(this.num != null){

      //   if(this.kindsNumber != undefined){
      //       for(let c of this.num){
      //         if(this.kindsNumber == c.kindsNumber){ var number = this.stockingNumber - c.stockingNumber} //和原数据对比 获取改动数量的差值
      //       }
      //       for(let a of this.saveList){ //不改变原数组数量
      //         if(this.kindsNumber == a.kindsNumber){
      //           a.stockingNumber = number
      //         }
      //           a.warehouseNo = this.warehouseNo
      //           a.receiptsNumber = this.receiptsNumber
      //           a.checkTime = this.checkDate
      //         for(let b of this.warehouseNoList){
      //             if(this.warehouseNo == b.value){
      //               this.warehouseName = b.label
      //             }
      //         }
      //       }
      //   }

      for (var q of this.tabList) {
        if (this.kindsNumber != undefined) {
          if (this.kindsNumber == q.kindsNumber) {
            q.stockingNumber = this.stockingNumber
          }
          q.warehouseNo = this.warehouseNo
          q.receiptsNumber = this.receiptsNumber
          q.checkTime = this.checkDate
          for (let s of this.warehouseNoList) {
            if (this.warehouseNo == s.value) {
              this.warehouseName = s.label
            }
          }
        }
      }
      // }
    },
    odChange() {
      this.cc = 'odChange'
      this.baocun()
    },
    Save() {
      this.cc = 'Save'
      this.baocun()
    },
    baocun() {
      if (this.stockingNumber == '') {
        this.$message.warning({ message: '请选择一条数据修改', offset: 400 })
        return
      }
      // console.log(this.saveList)

      var io = []
      var ip = []
      var iu = {}
      for (let w of this.tabList) {
        console.log(w.kindsNumber)
        io.push(w.kindsNumber)
        ip.push(w.stockingNumber)

        for (var f in io) {
          for (var g in ip) {
            if ((f = g)) {
              iu[io[f]] = Number(ip[g])
            }
          }
        }
        console.log(iu)
      }

      var params1 = {
        checkDate: this.checkDate + ' 23:59:00',
        receiptsNumber: this.receiptsNumber,
        warehouseNo: this.warehouseNo,
        totalNumber: this.totalNumber,
        // kindsNumber:this.kindsNumber,
        sellingPrice: this.sellingPrice,
        stockingNumber: Number(this.stockingNumber),
        onePrice: this.onePrice,
        sequence: this.sequence,
        kindsNumberMap: iu
      }
      console.log(params1)
      this.$emit(this.cc, params1)
    }
  },
  watch: {
    receiptsNumber(a, b) {
      if (a) {
        this.receiptsNumber = a.toUpperCase().trim()
      }
    },
    kindsNumber(a, b) {
      if (a) {
        this.kindsNumber = a.toUpperCase().trim()
      }
    },
    showData(a, b) {
      console.log(this.showData)
      this.tabList = a.warehouseCheckList
      // this.saveList = JSON.parse(JSON.stringify(this.tabList))
      // this.saveList.forEach(e=>{
      //   e.stockingNumber = 0
      // })
      this.checkDate = a.checkTime.slice(0, 10)
      this.sequence = a.sequence
      this.warehouseNo = a.warehouseNo
      this.kindsNumber = a.kindsNumber
      console.log(this.checkDate)
    }
  }
}
</script>
<style scoped>
.changeod {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 1100px;
  height: 460px;
  z-index: 10;
  background-color: white;
  overflow: auto;
}
.mask {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
  opacity: 0.3;
  z-index: 9;
}
.changeod > div:first-child {
  padding: 10px 15px;
  font-weight: 700;
  display: flex;
  justify-content: space-between;
  background: #f4f4f4;
}
.c-content {
  padding: 10px 15px;
}
.c-content > div:first-child {
  padding: 10px;
  margin: 10px 0;
  background: #f4f4f4;
}
.c-content div > p {
  display: inline-block;
  white-space: nowrap;
  width: 80px;
  text-align: end;
  margin: 3px 6px 0 0;
}
.c-content > div:not(:nth-child(5)) {
  display: flex;
}
.c-content div:not(:first-child) {
  margin-bottom: 5px;
}
.c-content > div:last-child {
  margin-top: 10px;
  float: right;
}
</style>

<style>
.changeod .c-content div .el-input__inner {
  width: 160px;
  height: 30px;
}
.changeod .c-content div .el-input {
  width: 160px;
}
.changeod .c-content .el-input__icon {
  line-height: 30px;
}
</style>
